package com.lgx.common.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.lgx.common.web.Constants;

/**
 * session失效
 * 
 * @author acer
 *
 */
public class SessionTimeoutFilter implements Filter {

	protected Logger log = Logger.getLogger(this.getClass());
	
	// 登录URL
	public String loginUrl = null;

	@Override
	public void destroy() {
		this.loginUrl = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
//		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(false);
		
		if(session != null && Constants.SESSION_TIMEOUT_FLAG){
//			String path = req.getContextPath();
//			String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
//			res.sendRedirect(basePath+loginUrl);
		}
		
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		loginUrl = config.getInitParameter("loginUrl");
	}

}
